﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VitCode.WorkFlow.DataAccess;
using VitCode.WorkFlow.Entities;

namespace WorkFlowSettingApp
{
    public partial class WorkItemEdit : Form
    {
        public WorkItemEdit()
        {
            InitializeComponent();
        }

        public Guid KeyId { get; set; }

        WorkItem ObjectVal { get; set; }

        private void btnRole_Click(object sender, EventArgs e)
        {
            SelObject selObj = new SelObject();
            selObj.ListSql = "Select * from (select Id,Alias 别名,Name 名称,Memo 备注 from Frame_role) a";
            selObj.KeyProName = "Id";
            selObj.ClassName = "Role";
            selObj.Text = "角色选择";
            selObj.ShowDialog();
        }

        private void SetFixTypeItem()
        {
            var dt = new DataTable();
            dt.Columns.Add("Txt");
            dt.Columns.Add("Val");
            dt.Rows.Add("必选", WorkItem.FIX_NEED);
            dt.Rows.Add("可选", WorkItem.FIX_OPTIONAL);
            dt.Rows.Add("只读", WorkItem.FIX_READ_ONLY);

            cboFixType.DataSource = dt;
            cboFixType.DisplayMember = "Txt";
            cboFixType.ValueMember = "Val";

        }

        private void WorkItemEdit_Load(object sender, EventArgs e)
        {
            SetFixTypeItem();

            if(ObjectVal==null)
            {
                WorkFlowAccess acc = new WorkFlowAccess();
                var obj = acc.GetSingleObjectById(typeof (WorkItem).Name, KeyId.ToString());
                if (obj != null && obj is WorkItem)
                    ObjectVal = ((WorkItem) obj);
            }

            if(ObjectVal!=null)
            {
                txtName.Text = ObjectVal.Name;
                txtPrams.Text = ObjectVal.Prams;
                txtRole.Text = ObjectVal.OperateRole.Name;
                txtTargetClassName.Text = ObjectVal.TargetClassName;
                txtTargetURL.Text = ObjectVal.TargetClassName;
                cboFixType.SelectedValue = ObjectVal.FixType;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var acc = new WorkFlowAccess();
            acc.BeginTransaction();
            try
            {
                WorkItem wi = null;
                if (ObjectVal != null)
                    wi = ObjectVal;
                else
                    wi = new WorkItem();
                wi.Name = txtName.Text;
                wi.Prams = txtPrams.Text;
                wi.TargetClassName = txtTargetClassName.Text;
                wi.TargetUrl = txtTargetURL.Text;
                wi.FixType = cboFixType.SelectedValue.ToString();
                acc.SaveOrUpdate(wi);
                acc.Commit();
            }
            catch(Exception ex)
            {
                acc.RollBack();
                throw ex;
            }
            finally
            {
                acc.CloseSession();
            }
        }
    }
}
